﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace StartStopPause
{

    public enum StateEnum
    {
        Run,
        Stop,
        Pause
    }
    public partial class FrmMain : UIForm
    {
        public FrmMain()
        {
            InitializeComponent();

            this.btn_Pause.Enabled = false;
            this.btn_Stop.Enabled = false;
            this.btn_Start.Enabled = false;
        }

        private int workStep = 10;

        int setpCache = 0;

        CancellationTokenSource cts;

        bool isRunning = false;

        Stopwatch sw;


        private void btn_Initial_Click(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                AddLog("气缸松开");
                Thread.Sleep(1000);

                AddLog("X轴回原点");
                Thread.Sleep(1000);

                AddLog("Y轴回原点");
                Thread.Sleep(1000);

                AddLog("Z轴回原点");
                Thread.Sleep(1000);

                AddLog("整机初始化完成");
                workStep = 100;
                this.Invoke(new Action(() =>
                {
                    this.btn_Start.Enabled = true;
                    this.btn_Initial.Enabled = false;
                }));

            });


        }
        private void btn_Start_Click(object sender, EventArgs e)
        {
            //设备执行完初始化

            if (workStep == 100)
            {
                StateEnum stateEnum = StateEnum.Run;
            }

            this.btn_Pause.Enabled = true;
            this.btn_Stop.Enabled = true;

            cts = new CancellationTokenSource();

            var task = Task.Run(() =>
            {
                AutoProcess();

            }, cts.Token); 




        }

        private void AutoProcess()
        {
            sw = Stopwatch.StartNew();
            sw.Start();
            while (!cts.IsCancellationRequested)
            {
                isRunning = true;
                switch (workStep)
                {
                    case 100:
                        AddLog("气缸顶升");
                        Thread.Sleep(1000);
                        workStep = 110;
                        break;
                    case 110:
                        AddLog("气缸夹紧");
                        Thread.Sleep(1000);
                        workStep = 120;
                        break;
                    case 120:
                        AddLog("X轴去放料位");
                        Thread.Sleep(1000);
                        workStep = 130;
                        break;
                    case 130:
                        AddLog("Y轴去放料位");
                        Thread.Sleep(1000);
                        workStep = 140;
                        break;
                    case 140:
                        AddLog("Z轴下降");
                        Thread.Sleep(1000);
                        workStep = 150;
                        break;
                    case 150:
                        AddLog("气缸松开");

                        Thread.Sleep(1000);
                        workStep = 160;

                        break;

                    case 160:
                        AddLog("顶升缩回");

                        Thread.Sleep(1000);
                        workStep = 100;
                        cts.Cancel();
                        break;
                    default:
                        break;
                }



            }
            sw.Stop();
            TimeSpan sp = sw.Elapsed;
            AddLog($"总计用时:{sp}");
        }

        private void btn_Stop_Click(object sender, EventArgs e)
        {
            if (isRunning == true)
            {
                cts.Cancel();
                workStep = 0;

                this.btn_Start.Enabled = false;
                this.btn_Pause.Enabled = false;

                this.btn_Initial.Enabled = true;
            }

        }

        private void btn_Pause_Click(object sender, EventArgs e)
        {


            if (this.btn_Pause.Text == "暂停")
            {
                cts.Cancel();
                setpCache = workStep;//步序缓存
                this.btn_Pause.Text = "继续";
                AddLog($"设备暂停中，当前步序：{workStep}");
            }
            else
            {
                workStep = setpCache;

                this.btn_Pause.Text = "暂停";
                cts = new CancellationTokenSource();


                var task = Task.Run(() =>
                {
                    AutoProcess();

                }, cts.Token);
            }

        }




        #region 添加日志
        //系统当前时间
        private string CurrentTime
        {
            get { return DateTime.Now.ToString("HH:mm:ss"); }
        }

        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="info"></param>
        public void AddLog(string info)
        {
            if (this.lst_Info.InvokeRequired)
            {
                this.lst_Info.Invoke(new Action(() =>
                {
                    ListViewItem listViewItem = new ListViewItem(CurrentTime);

                    listViewItem.SubItems.Add(info);
                    this.lst_Info.Items.Add(listViewItem);
                    this.lst_Info.Items[this.lst_Info.Items.Count - 1].EnsureVisible();


                }));
            }
            else
            {
                ListViewItem listViewItem = new ListViewItem(CurrentTime);

                listViewItem.SubItems.Add(info);
                this.lst_Info.Items.Add(listViewItem);
                this.lst_Info.Items[this.lst_Info.Items.Count - 1].EnsureVisible();


            }

        }




        #endregion


    }
}
